////////////////////////////////////////
// Autor:Mato Prorams 2010            //
// (C) Mato Programs 2010             //
////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MBMC.Dialogs
{
    public partial class PrimitiveEditor : Form
    {
        string file;

        public PrimitiveEditor(string filetoload)
        {
            InitializeComponent();
            if (filetoload != "::open::")
            {
                file = filetoload;
                richTextBox1.LoadFile(filetoload, RichTextBoxStreamType.PlainText);
                System.IO.FileInfo fiiii = new System.IO.FileInfo(filetoload + ".mbmc");
                if (fiiii.Exists == false)
                {
                    richTextBox1.SaveFile(filetoload + ".mbmc", RichTextBoxStreamType.PlainText);
                }
            }
            else
            {
                openFolderToolStripMenuItem.Enabled = true;
                //fileToolStripMenuItem.ShowDropDown();
                // = true;
                //openFolderToolStripMenuItem.Select();
            }
        }

        private void PrimitiveEditor_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SaveFile(file, RichTextBoxStreamType.PlainText);
            
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            richTextBox1.SaveFile(file + ".autorecovery" + DateTime.Now.Day + DateTime.Now.Minute + DateTime.Now.Hour + DateTime.Now.Month + DateTime.Now.Year + ".mbmc", RichTextBoxStreamType.PlainText);
            richTextBox1.SaveFile(file + ".autorecovery.mbmc", RichTextBoxStreamType.PlainText);
            //System.IO.File.CreateText(file + "ac.(Day)(Minute)(Hour)(Month)(Year).mbmc");
        }

        private void oPenLastAutorecoveryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.LoadFile(file + ".autorecovery.mbmc", RichTextBoxStreamType.PlainText);
            }
            catch
            {
                autoRecoveryCreatedEvery2MinutesToolStripMenuItem.ForeColor = Color.Red;
            }
        }

        private void originalFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile(file + ".mbmc", RichTextBoxStreamType.PlainText);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Redo();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void fontDialog1_Apply(object sender, EventArgs e)
        {
            richTextBox1.Font = fontDialog1.Font;
            //
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
        }

        private void PrimitiveEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            /*
            Regex keyWords = new Regex(@"\b(abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|" + "foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|" + "string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile|void|while|)\b");
            //Highlight every found word from keyWords.

            //Get the last cursor position in the richTextBox1.

            int selPos = richTextBox1.SelectionStart;

            //For each match from the regex, highlight the word.
            foreach (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
            {

                richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
                richTextBox1.SelectionColor = Color.Blue;
                richTextBox1.SelectionStart = selPos;
                richTextBox1.SelectionColor = Color.Black;
            }

            Regex keyWords2 = new Regex("//");
            //Highlight every found word from keyWords.

            //Get the last cursor position in the richTextBox1.

            int selPos2 = richTextBox1.SelectionStart;

            //For each match from the regex, highlight the word.
            foreach (Match keyWordMatch in keyWords2.Matches(richTextBox1.Text))
            {

                richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
                richTextBox1.SelectionColor = Color.Green;
                richTextBox1.SelectionStart = selPos2;
                richTextBox1.SelectionColor = Color.Black;
            }
             */
        }
    }
}